package com.chen10.cattle.config;

import com.chen10.cattle.config.interceptor.JwtInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Value("${project.avatar-path}")
    private String avatarPath;

    @Resource
    private JwtInterceptor jwtInterceptor;

    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(this.jwtInterceptor).addPathPatterns(new String[] { "/api/**" })
                .excludePathPatterns(new String[] {
                        "/api/auth/login",
                        "/api/auth/register",
                        "/api/email/send",
                        "/api/user/forgetPwd",
                        "/api/matchFight/matchSuccess",
                        "/api/cattle/upload/photo"  // 添加图片上传接口到排除列表
                });
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 确保没有重复的映射
        if (!registry.hasMappingForPattern("/avatar/**")) {
            registry.addResourceHandler("/avatar/**")
                    .addResourceLocations("file:" + avatarPath + "/");
        }
    }
}
